package cn.edu.ecut.se2024.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ecut.se2024.model.UserMenu;
import cn.edu.ecut.se2024.service.SysService;
import jakarta.servlet.http.HttpServletRequest;

@Controller
public class UnauthorizedSysControlloer {
	@Autowired
	private SysService sysService;

	@PostMapping("/sys.login")
    public String login(HttpServletRequest request, @RequestParam @Validated String name, @RequestParam @Validated String pswd) {
		UserMenu userMenu = sysService.login(name, pswd);	
		if (userMenu != null) {
			request.getSession().setAttribute(SysController.SYS_USER_MENU, userMenu);
		} else {
			request.setAttribute(SysController.SYS_LOGIN_FAILED, "用户名或密码不正确，登录失败！");
		}
        return "index"; 
    }

	@RequestMapping("/sys.home")
    public String home() {
        return "index"; 
    }

	@RequestMapping("/sys.quit")
    public String login(HttpServletRequest request) {
		request.getSession().invalidate();
        return "index"; 
    }
}
